#!/bin/bash

#-------------------------------------------------------------------------------
# Installation return codes are:
#
# 0 - no error
# 10 - usage error
# 20 - invalid/non-existent directory on HMC
# 30 - install file not found
# 40 - RPM test verify install error
# 50 - RPM install error
# 60 - 'tar' command failure
#
# Except for call rpm command to query, all calls to rpm must go
# go through dorpm, eraserpm etc..
#
# *The actual value returned will be 1x, 2x, etc, where x will be either 0 or 4
# which is based on any RPM un-install errors. Else, '0' if successful completion
#-------------------------------------------------------------------------------

#-------------------------------------------------------------------------------
# Initialize script return code, log file name
#-------------------------------------------------------------------------------
exitRC=0
logFile=/tmp/hmc_install.log

#-------------------------------------------------------------------------------
# Common exit point
#-------------------------------------------------------------------------------
exit_cleanup() {

   # keep the log file, but ensure a different user can overwrite it next time
   chmod 666 $logFile

   if [ $1 -ne 0 ]
   then
      exit $1$exitRC
   else
      exit 0
   fi
}

#-------------------------------------------------------------------------------
# RPM  install
#-------------------------------------------------------------------------------
dorpm() {

   if [ -f /opt/hsc/data/config/NO_UPDATE_RPMS ]
   then
     x="$2"
# Strip version then leading directory name
     f=`echo ${x%%-[0-9]*}`
     r=`echo ${f##*/}`
     for i in `cat /opt/hsc/data/config/NO_UPDATE_RPMS`
     do
       if [ "$r" = "$i" ]
       then
          return
       fi
     done
   fi
     
   # Log the test install output. The format for the "normal" RPM install
   # processing inside this script is 'rpm -i <file spec> --force --nodeps'
   rpm $* 2>> $logFile
   RC=$?
   return $RC
}

#-------------------------------------------------------------------------------
# RPM erase rpm
#-------------------------------------------------------------------------------
eraserpm() {

# If rpm is part of no update list then
# do not do anything with it

   if [ -f /opt/hsc/data/config/NO_UPDATE_RPMS ]
   then
     x="$2"
     for i in `cat /opt/hsc/data/config/NO_UPDATE_RPMS`
     do
       if [ "$x" = "$i" ]
       then
          return
       fi
     done
   fi
   rpm -v $* 2>>$logFile
   if [ $? -ne 0 ]
   then
      exitRC=4
      echo "error removing rpm fileset named $2" >> $logFile
   fi
}

#-------------------------------------------------------------------------------
# RPM test install
#-------------------------------------------------------------------------------
testrpm() {
   rpm --test  $1 $2 $3 $4
   return $?
}

#-------------------------------------------------------------------------------
# Start the product install...
#-------------------------------------------------------------------------------
cd /
image=$1
if [ "$image" = "" ]
then
   echo "Please specify directory containing installable packages"
   echo "usage: installImages  <directory>"
   exit_cleanup 1
fi

# check if directory exists
if [ ! -d $image ]
then
 echo "The directory $patchdir doesn't exist"
 echo "Please specify directory containing the installable packages."
 exit_cleanup 2
fi

image=$1

if [ -f /opt/hsc/data/config/NO_UPDATE_FILES ]
then
   rm -f /tmp/saved_files.tar
   cat /opt/hsc/data/config/NO_UPDATE_FILES | xargs tar -cvf /tmp/saved_files.tar
fi

image=$1

PATH=$PATH:/opt/IBMJava/jre/bin:
LD_LIBRARY_PATH=/opt/hsc/lib:/opt/hsc/lib/hcmjni:/lib:/usr/lib:$LD_LIBRARY_PATH
export PATH LD_LIBRARY_PATH

if [ ! -d /opt/IBMJava ]
then
   ln -s /opt/IBMJava2-141 /opt/IBMJava
fi

echo "--- Installing Common Console Framework...."
dorpm -e ccfw --nodeps --allmatches
#rm -rf /opt/ccfw
dorpm -ivh $image/baseHMC/ccfw*.rpm --force --nodeps

echo "--- Installing WebSM...."
dorpm -Uvh $image/websm/sysmgt.websm.framework*.rpm --force --nodeps
dorpm -Uvh $image/websm/sysmgt.websm.apps*.rpm --force --nodeps

echo "--- Installing HMC packages..."

# Save security information
# This will get processed in next reboot

sp=`grep ^SocketProvider /opt/hsc/data/cim.properties`
if [[ $sp == *PKCS12SocketProvider ]]; then
   echo $sp > /var/hsc/tmp/cimomcfg.properties.reboot
fi

dorpm -Uvh $image/baseHMC/IBMhsc.plfmgt*.rpm --force --nodeps 2>>$logFile
dorpm -Uvh $image/baseHMC/IBMhsc.coreserver*.rpm --force --nodeps 2>>$logFile
dorpm -Uvh $image/baseHMC/IBMhsc.cimprovider*.rpm --force --nodeps 2>>$logFile
dorpm -Uvh $image/baseHMC/IBMhsc.bundles_*.rpm --force --nodeps 2>>$logFile

#if [ -f $image/service/IBMinvscout*rpm ]
#then
#   echo "--- Installing InventoryScout packages...."
#   dorpm -Uvh $image/service/IBMinvscout*rpm --force --nodeps
#fi

echo "--- Installing InfoCenter packages...."

if [ -d $image/info ]
then
  dorpm -Uvh $image/info/IBMhmc.InfoCenter*rpm --force --nodeps
fi

if [ ! -L /usr/lib/libnetchmcx.so ]
then
  ln -s /usr/lib/libnetc.so /usr/lib/libnetchmcx.so
fi

if [ -f $image/hsc.tar ]
then
   echo "--- Installing miscellaneous updates...." 
   cd /
   tar -xf $image/hsc.tar
   if [ $? -ne 0 ]
   then
      exit_cleanup 6
   fi
fi

if [ -f $image/X.tar ]
then
   echo "--- Installing X Server Config...."
   tar -xf $image/X.tar
   if [ $? -ne 0 ]
   then
      exit_cleanup 6
   fi
fi

if [ -f $image/websm.tar ]
then
   echo "--- Installing WebSM updates...."
   tar -xf $image/websm.tar
   if [ $? -ne 0 ]
   then
      exit_cleanup 6
   fi
fi


if [ -f /opt/hsc/data/config/NO_UPDATE_FILES ]
then
   if [ -f /tmp/saved_files.tar ]
   then
     cd /
     tar -xvf /tmp/saved_files.tar
     rm -f /tmp/saved_files.tar
   fi
fi
 
if [ -f $image/postinstall ]
then
   $image/postinstall
fi

if [ -f $image/.VERSION ]
then
  cat $image/.VERSION >> /opt/hsc/data/version
  cat $image/.VERSION > /root/.version
  cat $image/.VERSION > /home/hscroot/.version
  chmod 644 /home/hscroot/.version
  chown hscroot.hmc /home/hscroot/.version
fi

touch /etc/resolv.conf

exit_cleanup 0
